//VRInterface.js (c) 2015 
//write by 邹吉滔





	//   PicReader.LoadPic("./Model/Img/NormalsMap.jpg", this.m_NormapTexID, this.m_SyncTool);
var gMain;
function main() 
{
	gMain = new CShowNormalObj();
	gMain.Begin();
}

function CShowNormalObj()
{
	this.m_GL = GetGLHandle();
	this.m_TouchID = 0;
	this.m_TouchX = -1;
	this.m_TouchY = -1;
	this.m_BakTouchX;
	this.m_BakTouchY;
	this.m_TouchFlag = 0;
	this.m_Rotate = 0;
	this.m_RotateSpeed = 0.5;
	this.m_EmdModel;
	this.m_Camera;
	this.KeyState = null;
	this.m_WndWidth = 512;
	this.m_WndHeight = 512;
	this.m_LoadComp		= false;
}

CShowNormalObj.prototype.Begin = function()
{
	this.KeyState = new Uint32Array(4);
	var T = this;
	for(i =0; i < 4; i++)
	{
		this.KeyState[i] = 0;
	}
	var glEnv = new GLEnvironment();
	glEnv.SetEventVector(this);

	var gl = GetGLHandle();
	var canvas = GetCanvasHandle();
	var w = canvas.width = window.innerWidth;
	var h = canvas.height = window.innerHeight;
	this.m_WndWidth = w;
	this.m_WndHeight = h;

	window.onresize=function(){T.ResizeCanvas();};

	//canvas.addEventListener("touchstart",  TwoTouchStart, false);
	//canvas.addEventListener("touchmove", TwoTouchMove, false);
/*
	canvas.ontouchstart	= function(event){T.TouchStart(event);};
	canvas.ontouchend	= function(event){T.TouchEnd(event);};
	canvas.onmousedown 	= function(event){T.DoMouseDown(event);};
	canvas.onmousemove	= function(event){T.DoMouseMove(event);};
	canvas.onmouseup	= function(event){T.DoMouseUp(event);};
	canvas.onmousewheel = function(event){T.OnMouseWheel(event);};
	*/
	document.onkeydown 	= function(ev){T.KeyDown(ev, T.KeyState);};
	document.onkeyup 	= function(ev){T.KeyUp(ev, T.KeyState);};
	

	
	gl.clearColor(0.6, 0.6, 0.6, 1.0);
	gl.enable(gl.DEPTH_TEST);
	TRACE("WebGL OK");


	this.m_EmdModel = new ReadEmd(gl);
	this.m_EmdModel.InitShader();
	this.m_EmdModel.SetCompCB(OnReadEmdEnd);
	this.m_EmdModel.ReadEmd("./Model/WebModel.emd","./Model/Img/");
	var tpos = new CVector3(0,0,0);
	this.m_EmdModel.SetPos(tpos);
	this.m_EmdModel.SetScale(1.0);

	/*
	每个物体是否显示，通过控制其m_IsShow属性来决定
	*/

	this.m_Camera = new CCamera();
	var ase = canvas.width/canvas.height;
	//this.m_Camera.PositionCamera(-70,-14,86,14,-16,-16,0,1,0,ase);

	this.m_Camera.PositionCamera(0,6,67,0,0,0,0,1,0,ase);
	
	
	var Tthis = this;
	var tick = function() 
	{
		// Start drawing
		Tthis.draw(gl, Tthis.m_EmdModel, Tthis.m_Camera, Tthis.KeyState, canvas);
		requestAnimationFrame(tick, canvas);
	};
	tick();
}

function OnReadEmdEnd(event)
{
	gMain.OnReadEnd(event);
}

CShowNormalObj.prototype.draw = function(gl, tEmd, camera, keystate, tcanvas)
{
	this.CheckInput(keystate,camera);
	gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);  // Clear color and depth buffers
  	camera.Look();
 	gl.enable(gl.BLEND);//首先要启用混合
	gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
 	if(this.m_EmdModel.m_SyncTool.IsSync() === true)
 	{
 		this.m_Rotate++;
 		if(this.m_Rotate>-360)
 		{
 			this.m_Rotate = 0;
 		}
 		this.m_EmdModel.Rotate(1);
 		this.m_EmdModel.OwnerDraw(camera); 	
 	}
	return;
}

CShowNormalObj.prototype.ResizeCanvas = function()
{
	var canvas = GetCanvasHandle();
	var w = canvas.width = window.innerWidth;
	var h = canvas.height = window.innerHeight;
	this.m_WndWidth = w;
	this.m_WndHeight = h;

	var gl = getWebGLContext(canvas);
	gl.viewport(0, 0, w, h);
	this.m_Camera.m_Sspect = (w-22)/(h-20);
}























CShowNormalObj.prototype.TouchStart = function(event) 
{
	this.m_DragFlag = 1;
	this.m_TouchFlag = 1;

	var centerX = 200;
	var centerY = 550;
	var canvas = document.getElementById('webgl');
	event.preventDefault();
	var touch = event.touches[0];
	var x = touch.pageX;
	var y = touch.pageY;
	w = canvas.width;
	h = canvas.height;
	//canvas.webkitRequestFullScreen();
	
	this.m_TouchX = touch.pageX;
	this.m_TouchY = canvas.height - touch.pageY;
}

CShowNormalObj.prototype.TouchEnd = function(event)
{
	this.m_DragFlag = 0;
 	this.m_TouchID = 0;
 	this.m_TouchFlag = 0;
}


CShowNormalObj.prototype.DoMouseDown = function(event)
{
	this.m_DragFlag = 1;
	this.m_TouchFlag = 1;
	var centerX = 175;
	var centerY = 725;
	var canvas = document.getElementById('webgl');
	var bbox =canvas.getBoundingClientRect();
	var x = event.pageX;
	var y = event.pageY;
	x = x - bbox.left * (canvas.width / bbox.width);
	y = y - bbox.top * (canvas.height / bbox.height);

	this.m_TouchX = x;
	this.m_TouchY = canvas.height - y;

	this.m_BakTouchX = x;
	this.m_BakTouchY = this.m_TouchY;
	TRACE("CAMPOS:"+(this.m_Camera.m_vPosition.x).toFixed(2) + " "+ (this.m_Camera.m_vPosition.y).toFixed(2) + " "+ (this.m_Camera.m_vPosition.z).toFixed(2) +" "+ (this.m_Camera.m_vView.x).toFixed(2) +" "+ (this.m_Camera.m_vView.y).toFixed(2) + " " + (this.m_Camera.m_vView.z).toFixed(2));
}


CShowNormalObj.prototype.DoMouseMove = function(event)
{
	var canvas = document.getElementById('webgl');
	var bbox =canvas.getBoundingClientRect();
	var x = event.pageX;
	var y = event.pageY;
	x = x - bbox.left * (canvas.width / bbox.width);
	y = y - bbox.top * (canvas.height / bbox.height);
	this.m_TouchX = x;
	this.m_TouchY = canvas.height - y;


	// 旋转场景
	
	if(this.m_DragFlag === 1)
	{
		var tmpx = this.m_BakTouchX - this.m_TouchX;
		this.m_Camera.RotateView(tmpx/120.0,1);
		this.m_BakTouchX = this.m_TouchX;
	}
	
	
}

CShowNormalObj.prototype.DoMouseUp = function( event )
{
	this.m_DragFlag = 0;
	this.m_TouchFlag = 0;
	this.m_TouchID = 0;
}

CShowNormalObj.prototype.OnMouseWheel = function( event )
{
	var Speed = 1.5;
	var tarpos;
	if(event.wheelDeltaY>0)
	{
		this.m_WhellValue = 1.5;
	}
	else
	{
		this.m_WhellValue = -1.5;
	}
}


CShowNormalObj.prototype.OnReadEnd = function()
{

}



CShowNormalObj.prototype.KeyDown = function(ev, keystate)
{
	if(ev.keyCode === 77)
	{
		if(this.m_EmdModel.m_ShowNormalFlag === true)
		{
			this.m_EmdModel.m_ShowNormalFlag = false;
		}
		else
		{
			this.m_EmdModel.m_ShowNormalFlag = true;
		}
	}
	if(ev.keyCode === 65)
	{
		keystate[0] = 1;
	}
	if(ev.keyCode === 83)
	{
		keystate[1] = 1;
	}
	if(ev.keyCode === 68)
	{
		keystate[2] = 1;
	}
	if(ev.keyCode === 87)
	{
		keystate[3] = 1;
	}
}

CShowNormalObj.prototype.KeyUp = function(ev, keystate)
{
	if(ev.keyCode === 65)
	{
		keystate[0] = 0;
	}
	if(ev.keyCode === 83)
	{
		keystate[1] = 0;
	}
	if(ev.keyCode === 68)
	{
		keystate[2] = 0;
	}
	if(ev.keyCode === 87)
	{
		keystate[3] = 0;
	}
}



CShowNormalObj.prototype.CheckInput = function(keystate, camera)
{
	var Speed = 2.5;
	var RotSpeed = 0.06;
	var tarpos;


	

	if((this.m_TouchID === 1) || (keystate[3] === 1))
	{
		camera.MoveCamera(Speed,0);
	}
	
	if((this.m_TouchID === 2)||(keystate[1] === 1))
	{
		camera.MoveCamera(-1 * Speed,0);
	}
	
	if(this.m_TouchID === 3)
	{
		camera.RotateView(RotSpeed,1);
	}
	if(this.m_TouchID === 4)
	{
		camera.RotateView(-1*RotSpeed,1);
	}
	if(keystate[0] === 1)
	{
		camera.RotateView(-1*RotSpeed,1);
	}
	if(keystate[2] === 1)
	{
		camera.RotateView(RotSpeed,1);
	}
}


